package com.bpzj.util.regexp;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author bpzj 2019/8/30 14:57
 */
public class RegexConstants {

  public static final Pattern EMPTY_OR_NUMBER = Pattern.compile("^$|\\d+\\.?\\d*");

  /* ===========================  日期相关  ===========================  */
  static final String MONTH_31 = "((0?[13578])|(1[02]))-((0?[1-9])|([1-2][0-9])|(3[01]))";
  static final String MONTH_30 = "((0?[469])|(11))-((0?[1-9])|([1-2][0-9])|(30))";
  static final String MONTH_29 = "(0?2)-((0?[1-9])|([1-2][0-9]))";
  static final String DAY_OF_MONTH = "(" + MONTH_31 + "|" + MONTH_30 + "|" + MONTH_29 + ")";

  public static final Pattern EMPTY_OR_DATE = Pattern.compile("^$|\\d{4}-" + DAY_OF_MONTH);

  public static final Pattern EMPTY_OR_ZERO_TO_100 = Pattern.compile("^$|^([1-9]?\\d|100)$");

  /**
   * 0-100 带小数 或不带小数
   */
  public static final Pattern ZERO_TO_100_WITH = Pattern.compile("^([1-9]?\\d|100)(\\.\\d{1,4})?$");
  // 0.00% - 100%   或 0.0000-1
  public static final Pattern ZERO_TO_1_WITH = Pattern.compile("([1-9]?\\d(\\.\\d{1,2})?|100(\\.0{1,2})?)%|0(\\.\\d{1,4})?|1");

  // 中文
  public static final Pattern CHINESE_CHAR = Pattern.compile("[\\u4e00-\\u9fa5]");
  // 标点符号
  public static final Pattern PUNCTUATION_MARK = Pattern.compile("\\pP+");

  // (?=) 的用法, 不参与到 match 后的结果存放
  // 匹配 Windows
  public static final Pattern test = Pattern.compile("^Win(?=dows)dows.*(?<=ws)$");
  public static final Pattern password = Pattern.compile("^(?=.*[0-9])(?=.*[a-z])(?=.*[!~@]).{1,5}$");

  // 驼峰和下划线转换
  private String xx_xx_xx;

  public static final Pattern ldc = Pattern.compile("^((yyuap)|(yonyoucloud))$");

  public static final Pattern not_include = Pattern.compile("Long.*=\\s?(?!TypeUtils).*");

  public static void main(String[] args) {

    StringBuffer buffer = new StringBuffer();
    buffer.append("first line\r\n").append("second line\r\n").append(" 1: 10.124.3.1\r\n");
    String a = buffer.toString();
    // TODO 多行匹配
    Matcher matcher1 = Pattern.compile(".*\\n\\s{0,3}(\\d{1,3}):.*", Pattern.DOTALL).matcher(buffer);
    boolean matches = matcher1.matches();

    Matcher matcher = test.matcher("Windows");
    if (matcher.matches()) {
      System.out.println("windows 匹配到了" + test.pattern() +", 匹配的count=" + matcher.groupCount());
    }

    testChineseChar();

    testEmptyNumber();

    testZeroTo100();

    testDatePattern();

    // 去除首尾空格
    String str = "    首尾有多个空格   ";
    System.out.println(str.replaceAll("^ +", "").replaceAll(" +$", ""));

  }

  private static void testChineseChar() {
    System.out.println("-----------测试中文字符---------------");
    System.out.println(CHINESE_CHAR.matcher("1").matches());
    System.out.println(CHINESE_CHAR.matcher(".").matches());
    System.out.println(CHINESE_CHAR.matcher("a").matches());
    System.out.println(CHINESE_CHAR.matcher("z").matches());
    System.out.println(CHINESE_CHAR.matcher("：").matches());
    System.out.println("中：" + CHINESE_CHAR.matcher("中").matches());
    System.out.println("文：" + CHINESE_CHAR.matcher("文").matches());
  }

  private static void testZeroTo100() {
    System.out.println("--------------------------");
    System.out.println(ZERO_TO_100_WITH.matcher("1").matches());
    System.out.println(ZERO_TO_100_WITH.matcher("1.23").matches());
    System.out.println(ZERO_TO_100_WITH.matcher("1.1234").matches());
    System.out.println(ZERO_TO_100_WITH.matcher("101").matches());
    System.out.println(ZERO_TO_100_WITH.matcher("0").matches());
  }

  private static void testEmptyNumber() {
    System.out.println("--------------------------");
    System.out.println(EMPTY_OR_NUMBER.matcher("").matches());
    System.out.println(EMPTY_OR_NUMBER.matcher("1").matches());
    System.out.println(EMPTY_OR_NUMBER.matcher("1.").matches());
    System.out.println(EMPTY_OR_NUMBER.matcher("0.1").matches());
    System.out.println(EMPTY_OR_NUMBER.matcher("1.2").matches());
    System.out.println(EMPTY_OR_NUMBER.matcher(".").matches());
    System.out.println(EMPTY_OR_NUMBER.matcher("1.2瓶").matches());
  }

  public static void testDatePattern() {
    System.out.println("--------------------------");
    System.out.println(EMPTY_OR_DATE.matcher("2019-01-00").matches());
    System.out.println(EMPTY_OR_DATE.matcher("2019-00-01").matches());
    System.out.println(EMPTY_OR_DATE.matcher("2019-1-32").matches());
    System.out.println(EMPTY_OR_DATE.matcher("2019-04-31").matches());
    System.out.println(EMPTY_OR_DATE.matcher("2019-2-30").matches());

    System.out.println("--------------------------");
    System.out.println(EMPTY_OR_DATE.matcher("").matches());
    System.out.println(EMPTY_OR_DATE.matcher("2019-01-31").matches());
  }

}
